#include "playscene.h"
#include<QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QTimer>
#include<QMovie>
#include<QLabel>
#include<mycoin.h>
#include<dataconfig.h>
#include<QPropertyAnimation>
#include<QSoundEffect>
//PlayScene::PlayScene(QWidget *parent)
//    : QMainWindow{parent}
//{

//}
int t = 60;
int at = 20;
PlayScene::PlayScene(int levelNum)
{
    QString str = QString("进入的是第 %1 关").arg(levelNum);
                  qDebug() << str;
    QString str2 = QString("第 %1 关").arg(levelNum);
    this->levelIndex = levelNum;


    //初始化游戏场景
    //设置固定大小
    this->setFixedSize(320,588);
//    设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
//    设置标题
    this->setWindowTitle(str2);


    QMenuBar * bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //    创建退出菜单项
    QAction * quitAction = startMenu->addAction("退出游戏");

    //    点击退出 退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //添加音效  翻金币   胜利
    QSoundEffect * flipSound = new QSoundEffect(this);
    flipSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
    QSoundEffect * winSound = new QSoundEffect(this);
    winSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));

    //返回按钮

    MypushButton * backBtn = new MypushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn ->setParent(this);
    backBtn ->move(this->width() - backBtn->width(),this->height() - backBtn->height());

    //    点击返回按钮实现返回
    connect(backBtn,&MypushButton::clicked,[=](){
        qDebug() << "点击了游戏关卡的返回按钮";
        t = 60;
        at = 20;
        QTimer::singleShot(800,this,[=](){
            //发出自定义chooseSceneBack信号
            emit this->chooseSceneBack();
        });

    });
    //   加载倒计时图片
    QLabel *label3 = new QLabel(this);
    QMovie *movie = new QMovie(":/res/60sconed.gif");
    //label->resize(210,170);
    label3->setGeometry(QRect(220, 20, 100, 100));
    //label->move(130,20);
    label3->setMovie(movie);
    movie->setScaledSize(QSize(label3->width(),label3->height()));
    movie->start();
    label3->show();

    dataConfig config;
//    初始化每一个二维数组
    for(int i = 0; i < 4; i ++)
        for(int j = 0; j < 4; j ++)
        {
            this->gameArray[i][j] = config.mData[this->levelIndex][i][j];
        }



//    显示当前关卡数
    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(16);
    QString str3 = QString ("level: %1").arg(this->levelIndex);
//    将字体样式设置进标签中
    label->setFont(font);
    label->setText(str3);
    label->setGeometry(20,this->height() - 50,120,50);

    //    显示剩余步骤数
    QLabel * label2 = new QLabel;
    label2->setParent(this);
    QFont font2;
    font2.setFamily("华文新魏");
    font2.setPointSize(16);
    QString str4 = QString ("剩余步数: %1").arg(at);
    qDebug() << "剩余步数:" << at;
    label2->setFont(font2);
    label2->setText(str4);
    label2->setGeometry(100,this->height() - 50,120,50);

//    胜利图片显示
    QLabel * winLabel = new QLabel;
    QPixmap tmpix;
    tmpix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpix.width(),tmpix.height());
    winLabel->setPixmap(tmpix);
    winLabel->setParent(this);
    winLabel->move((this->width() - tmpix.width())*0.5,-tmpix.height());

    QTimer *timer = new QTimer(this);
    timer->start(1000);
    timer->setInterval(1000);
    connect(timer,&QTimer::timeout,[=](){
        t--;
        qDebug() << t;
        if(t < 0)
        {
            qDebug() << "游戏失败了！";
            t = 60;
            at = 20;
            timer->stop();
            QTimer::singleShot(800,this,[=](){
                //发出自定义chooseSceneBack信号
                emit this->chooseSceneBack();
            });
        }
    });

//    显示金币背景图
    for(int i = 0; i < 4; i ++)
        for(int j = 0; j < 4; j ++)
        {
//            绘制背景图片
            QLabel * label = new QLabel;
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->setParent(this);
            label->move(57 + i * 50, 200 + j * 50);

            //    创建金币
            QString  str;
            if(this->gameArray[i][j] == 1)
            {
//                显示金币
                str = ":/res/Coin0001.png";
            }
            else {
                str = ":/res/Coin0008.png";
            }
            MyCoin *coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(59 + i * 50,204 + j * 50);

            //给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = gameArray[i][j];
//            将金币放入到 金币的二维数组里 以便以后期的维护
            coinBtn[i][j] = coin;
            //点击金币进行翻转
            connect(coin,&MyCoin::clicked,[=](){
                flipSound->play();
                coin->changeFlag();

                at--;
                QString str4 = QString ("剩余步数: %1").arg(at);
                qDebug() << "剩余步数:" << at;
                label2->setFont(font2);
                label2->setText(str4);
                label2->setGeometry(105,this->height() - 50,120,50);

                if(at <= 0)
                {
                    qDebug() << "游戏失败了！";
                    at = 20;
                    t = 60;
                    timer->stop();
                    QTimer::singleShot(800,this,[=](){
                        //发出自定义chooseSceneBack信号
                        emit this->chooseSceneBack();
                    });
                }
                this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0;

//                翻转周围金币  延时翻转
                QTimer::singleShot(300,this,[=](){
                    int x[] = {0,1,0,-1},y[] = {-1,0,1,0};
                    for(int k = 0; k < 4; k ++)
                    {
                        int I = i + x[k];
                        int J = j + y[k];
                        if(I >=0 && I <= 3 && J >=0 && J <=3)
                        {
                            coinBtn[I][J]->changeFlag();
                            this->gameArray[I][J] = this->gameArray[I][J] == 0 ? 1 : 0;
                        }
                    }
                    //判断是否胜利
                    this->isWin = true;
                    for(int i = 0; i < 4; i ++)
                        for(int j = 0; j < 4; j ++)
                        {
                            if(coinBtn[i][j]->flag == false)
                            {
                                isWin = false;
                                break;
                            }
                        }

                    if(this->isWin == true)
                    {
                        qDebug() << "游戏胜利了！";
                        t = 60;
                        at = 20;
                        winSound->play();
                        for(int i = 0; i < 4; i ++)
                            for(int j = 0; j < 4; j ++)
                            {
                                coinBtn[i][j]->isWin = true;
                            }

                        //将胜利的图片移动下来
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                        //设置时间的间隔
                        animation->setDuration(1000);
                        //设置开始的位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置结束的位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y() + 114,winLabel->width(),winLabel->height()));
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });
            });
        }
}
void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;

    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //    加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap(10,30,pix.width() * 0.5,pix.height()*0.5,pix);



}














